package gui;

import entities.Student;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import commands.AssessRisk;
import commands.LoginCheckCommand;
import containers.StudentContainer;

/**
 * 
 * @author eks691
 * @preconditions student has a cosigner
 */
public class EmployeeRiskAssessmentPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	/**
	 * Student the panel is displaying
	 */
	private Student student;

	/**
	 * id of the student, employee, manager in the system
	 */
	private int id;

	/**
	 * the risk for the loan
	 */
	private float risk;

	/**
	 * True if the loan should be approved; false otherwise
	 */
	boolean approve;

	public EmployeeRiskAssessmentPanel(int studentNum, int i) {
		id = i;
		Student s = StudentContainer.dictionary().get(studentNum);
		student = s;
		/*
		 * System.out.println("Risk Assessment; Name of Student: " +
		 * student.getName());
		 * 
		 * System.out.println("CoSigner: " +
		 * student.getLoan().getCosigner().getName());
		 */

		// setLayout(new BorderLayout());
		// panels are set out horizontally
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		setBorder(new TitledBorder(new EtchedBorder(), "Risk Assessment"));

		add(Box.createVerticalGlue());
		add(Box.createVerticalGlue());

		DisplayInfoPanel displayInfo = new DisplayInfoPanel();
		add(displayInfo);
		add(new GrossDebtRatioPanel());
		add(new RecommendationsPanel());
		add(Box.createVerticalGlue());
		add(Box.createVerticalGlue());
		add(Box.createVerticalGlue());
		add(Box.createVerticalGlue());
		add(Box.createVerticalGlue());
		add(Box.createVerticalGlue());
		add(new ButtonPanel(), BorderLayout.PAGE_END);

	}

	private class DisplayInfoPanel extends JPanel {

		private static final long serialVersionUID = 1L;

		/**
		 * Construct a panel with the current information for the current
		 * student
		 */
		private DisplayInfoPanel() {
			setBorder(new TitledBorder(new EtchedBorder(), "Loan Information"));

			setLayout(new GridLayout(4, 2));

			add(new JLabel("Name: " + student.getName()));
			add(new JLabel("Monthly Expenses: "
					+ student.getFinances().getMonthlyExpenses()));
			add(new JLabel("Co-Signer Name: "
					+ student.getLoan().getCosigner().getName()));
			add(new JLabel("Monthly Income: "
					+ student.getFinances().getMonthlyIncome()));
			AssessRisk ar = new AssessRisk(student);
			risk = ar.doRiskAssessment();
			// format float
			DecimalFormat df1 = new DecimalFormat("####.00");
			add(new JLabel("Gross-Debt Ratio:   0" + df1.format(risk)));
			add(new JLabel("Co-Signer Monthly Expenses: "
					+ student.getLoan().getCosigner().getFinancialInfo()
							.getMonthlyExpenses()));
			add(new JLabel(""));
			add(new JLabel("Co-Signer Monthly Income: "
					+ student.getLoan().getCosigner().getFinancialInfo()
							.getMonthlyIncome()));
		}
	}

	private class GrossDebtRatioPanel extends JPanel {

		private static final long serialVersionUID = 1L;

		private GrossDebtRatioPanel() {
			setBorder(new TitledBorder(new EtchedBorder(), "Gross-Debt Ratios"));

			setLayout(new GridLayout(3, 2, 10, 10));

			add(new JLabel("Non-Risky: "));
			add(new JLabel("< 0.29"));
			add(new JLabel("Normal: "));
			add(new JLabel("0.30-0.39"));
			add(new JLabel("Risky: "));
			add(new JLabel("> 0.40"));
		}
	}

	private class RecommendationsPanel extends JPanel {

		private static final long serialVersionUID = 1L;

		private RecommendationsPanel() {
			setBorder(new TitledBorder(new EtchedBorder(), "Recommendations"));

			setLayout(new GridLayout(1, 1));

			if (risk < 0.29) {
				add(new JLabel("Approve Loan.  The co-signer can more than "
						+ "guarantee the loan."));
				approve = true;
			} else if (risk >= 0.30 && risk <= 0.39) {
				add(new JLabel("Approve Loan. The co-signer will most likely"
						+ " be able to guarantee the loan."));
				approve = true;
			} else if (risk >= 0.40) {
				add(new JLabel("Decline Loan.  The co-signer cannot "
						+ "guarantee the loan."));
				approve = false;
			}
		}
	}

	private class ButtonPanel extends JPanel {

		private static final long serialVersionUID = 1L;

		private ButtonPanel() {
			setLayout(new GridLayout(1, 2, 150, 150));

			JButton backButton = new JButton("Back");
			backButton.setFont(new Font("Serif", Font.BOLD, 24));

			add(new JLabel(""));
			add(new JLabel(""));
			add(backButton);

			backButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent event) {
					getTopLevelAncestor().setVisible(false);
					MainMenuFrame frame = new MainMenuFrame(
							LoginCheckCommand.UserType.EMPLOYEE, id);
					frame.setVisible(true);
					EmployeeNewStudentLoanInfoPanel newPanel = new EmployeeNewStudentLoanInfoPanel(
							student, id);
					frame.changePanel(newPanel);
				}
			});
		}
	}

}
